package com.ysxsoft.wdg.ui.activity.my;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.my.SyAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.response.HomeProductResponse;
import com.ysxsoft.wdg.bean.response.ShouYiDetailResponse;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.ysxsoft.wdg.util.statusbar.StatusBarUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

public class SyActivity extends BaseActivity {

    public static void start(Context context,int type,String money){
        Intent intent = new Intent(context,SyActivity.class);
        intent.putExtra("type",type);
        intent.putExtra("money",money);
        context.startActivity(intent);
    }

    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;

    private int type = 1;//1本月预估  2今日收益  3上月结算   4一级返利   5团队奖励  6代理奖励
    private SyAdapter adapter;
    private String m="";//钱

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sy;
    }

    @Override
    protected void initData() {
        StatusBarUtil.setStatusBarTextColor(this,true);
        initStatusBar(topView,true);

        Intent intent = getIntent();
        type = intent.getIntExtra("type",1);
        m = intent.getStringExtra("money");
        tvMoney.setText(m==null?"":m);

        String title = "本月预估";
        String name = "本月预估收益";
        switch (type){
            case 1:
                title = "本月预估";
                name = "本月预估收益";
                break;
            case 2:
                title = "今日收益";
                name = "今日收益金额";
                break;
            case 3:
                title = "上月结算";
                name = "上月结算金额";
                break;
            case 4:
                title = "一级奖励";
                name = "一级奖励金额";
                break;
            case 5:
                title = "团队奖励";
                name = "团队奖励金额";
                break;
            case 6:
                title = "代理奖励";
                name = "代理奖励金额";
                break;
        }

        titleContent.setText(title);
        tvName.setText(name);

//        tvMoney.setText(getString(R.string.price_rmb,"1000"));


        adapter = new SyAdapter(R.layout.item_sy);
        adapter.setType(type);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.setAdapter(adapter);

        getList();
    }

    @Override
    protected void setListener() {
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                getList();
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page=1;
                getList();
            }
        });
    }


    @OnClick(R.id.title_finish)
    public void onViewClicked() {
        finish();
    }

    private int page = 1;

    /**
     * 获取我的收益详情
     */
    private void getList() {
        OkHttpUtils.post()
                .url(UrlHost.ACCOUNT_SHOU_LIST)
                .addParams("uid",  SharePrefUtils.getUserId())
//                .addParams("uid",  "567")
                .addParams("page", page + "")
                .addParams("type", type+"")//1本月预计收益明细2今日收益明细3上月结算明细4一级奖励明细5团队奖励明细6代理奖励明细
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                    multStatusView.showError();
                    reset();
                }
            }

            @Override
            public void onResponse(String response, int id) {
                reset();
                if (response != null) {
                    ShouYiDetailResponse shouYiDetailResponse = JsonUtils.parseByGson(response, ShouYiDetailResponse.class);
                    if (shouYiDetailResponse == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(shouYiDetailResponse.getCode())) {
                        List<ShouYiDetailResponse.DataBean> list = shouYiDetailResponse.getData();
                        if (page == 1) {
                            multStatusView.showContent();
                            if (list != null && !list.isEmpty()) {
                            } else {
                                multStatusView.showEmpty();
                            }
                            adapter.setNewData(list);
                        } else {
                            adapter.addData(list);
                        }
                    }
                } else {
                    multStatusView.showError();
                }
            }
        });
    }

    private void reset() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh(true);
        }
        refreshLayout.finishLoadmore();
    }
}
